package com.mazaiting.generator.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mazaiting.common.core.domain.result.Option;
import com.mazaiting.common.core.domain.result.ResultCode;
import com.mazaiting.common.core.ex.exceptions.BaseException;
import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.generator.converter.ITemplateGroupConverter;
import com.mazaiting.generator.domain.entity.TemplateGroupEntity;
import com.mazaiting.generator.domain.form.CloneGroupForm;
import com.mazaiting.generator.domain.form.TemplateGroupForm;
import com.mazaiting.generator.domain.query.TemplateGroupQuery;
import com.mazaiting.generator.domain.vo.TemplateGroupVO;
import com.mazaiting.generator.mapper.ITemplateGroupMapper;
import com.mazaiting.generator.service.ITemplateGroupService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 模板组
 *
 * @author mazaiting
 * @datetime 2022-11-11 23:12:08
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TemplateGroupServiceImpl extends ServiceImpl<ITemplateGroupMapper, TemplateGroupEntity> implements ITemplateGroupService {

    private final ITemplateGroupConverter templateGroupConvert;

    @Override
    public BasePageVO<TemplateGroupVO> page(TemplateGroupQuery templateGroupQuery) {
        // 分页数据
        Page<TemplateGroupEntity> page = lambdaQuery()
            .page(new Page<>(templateGroupQuery.getPageNum(), templateGroupQuery.getPageSize()));
        // entity -> vo
        Page<TemplateGroupVO> voPage = templateGroupConvert.entity2VO(page);

        return BasePageVO.success(voPage);
    }

    @Override
    public boolean save(TemplateGroupForm templateGroupForm) {
        // form -> entity
        TemplateGroupEntity entity = templateGroupConvert.form2Entity(templateGroupForm);
        entity.setId(null);
        // 如果是选择的状态, 则要更新其他数据状态为 false
        if (entity.getSelected()) {
            lambdaUpdate()
                    .set(TemplateGroupEntity::getSelected, false)
                    .eq(TemplateGroupEntity::getSelected, true)
                    .update();
        }
        return save(entity);
    }

    @Override
    public boolean update(TemplateGroupForm templateGroupForm){
        // 根据 ID 查询查询实体
        TemplateGroupEntity entity = getById(templateGroupForm.getId());
        if (Objects.isNull(entity)) {
            log.warn("实体不存在");
        }
        // form -> entity
        templateGroupConvert.form2Entity(templateGroupForm);
        // 如果是选择的状态, 则要更新其他数据状态为 false
        if (entity.getSelected()) {
            lambdaUpdate()
                    .set(TemplateGroupEntity::getSelected, false)
                    .eq(TemplateGroupEntity::getSelected, true)
                    .update();
        }
        return updateById(entity);
    }

    @Override
    public boolean deleteByIds(String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        return removeByIds(idList);
    }

    @Override
    public TemplateGroupVO getInfoById(Long id) throws EmptyObjectException {
        TemplateGroupEntity templateGroupEntity = getById(id);
        if (Objects.isNull(templateGroupEntity)) {
            log.warn("实体信息不存在");
            throw new EmptyObjectException(ResultCode.COMMON_FAIL.getCode(), "模板组信息不存在");
        }
        return templateGroupConvert.entity2VO(templateGroupEntity);
    }

    @Override
    public List<Option<Long>> listOptions() throws BusinessException {
        // 获取选项列表
        List<Option<Long>> optionList = lambdaQuery()
                .select(TemplateGroupEntity::getId, TemplateGroupEntity::getName)
                .list()
                .stream().map(entity -> new Option<>(entity.getId(), entity.getName())).toList();
        if (CollectionUtils.isEmpty(optionList)) {
            throw new BusinessException(ResultCode.COMMON_FAIL.getCode(), "无可用模板组, 请先添加模板组");
        }
        return optionList;
    }

}